﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Caliburn.Micro;
using Microsoft.Phone.Controls;
using System.Collections.Generic;
using CetWin.ViewModels;
using CetWin.Phone.Services;
using Phone.Framework.Config;
using Phone.Framework;

namespace CetWin
{
    public class AppBootstrapper : PhoneBootstrapper
    {
        PhoneContainer container;

        protected override void Configure()
        {
            container = new PhoneContainer(RootFrame);
            //IoC
            container.RegisterPhoneServices();
            container.PerRequest<MainPageViewModel>();
            container.PerRequest<HistoriesViewModel>();
            container.PerRequest<CategoryViewModel>();
            container.PerRequest<WordDetailViewModel>();
            container.PerRequest<SettingsViewModel>();
            container.PerRequest<SaveCategoryViewModel>();
            container.PerRequest<AboutViewModel>();
            container.Singleton<HistoryService>();
            container.PerRequest<SearchRecordService>();
            container.PerRequest<WordService>();
            container.PerRequest<CategoryService>();
            container.Singleton<CetWinSetting>();
            container.RegisterPerRequest
                (typeof(IStorageMechanism), "AppSettingsStorageMechanism", typeof(AppSettingsStorageMechanism));

            container.Singleton<SettingManager>();
            container.RegisterSingleton(typeof(IMediaPlayService), null, typeof(MediaPlayService));
            AddCustomConventions();
        }

        static void AddCustomConventions()
        {
            //ConventionManager.AddElementConvention<Pivot>(Pivot.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
            //    (viewModelType, path, property, element, convention) =>
            //    {
            //        if (ConventionManager
            //            .GetElementConvention(typeof(ItemsControl))
            //            .ApplyBinding(viewModelType, path, property, element, convention))
            //        {
            //            ConventionManager
            //                .ConfigureSelectedItem(element, Pivot.SelectedItemProperty, viewModelType, path);
            //            ConventionManager
            //                .ApplyHeaderTemplate(element, Pivot.HeaderTemplateProperty, viewModelType);
            //            return true;
            //        }

            //        return false;
            //    };

            //ConventionManager.AddElementConvention<Panorama>(Panorama.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
            //    (viewModelType, path, property, element, convention) =>
            //    {
            //        if (ConventionManager
            //            .GetElementConvention(typeof(ItemsControl))
            //            .ApplyBinding(viewModelType, path, property, element, convention))
            //        {
            //            ConventionManager
            //                .ConfigureSelectedItem(element, Panorama.SelectedItemProperty, viewModelType, path);
            //            ConventionManager
            //                .ApplyHeaderTemplate(element, Panorama.HeaderTemplateProperty, viewModelType);
            //            return true;
            //        }

            //        return false;
            //    };
        }

        protected override object GetInstance(Type service, string key)
        {
            return container.GetInstance(service, key);
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            return container.GetAllInstances(service);
        }

        protected override void BuildUp(object instance)
        {
            container.BuildUp(instance);
        }

        protected override void OnUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // An unhandled exception has occurred; break into the debugger
                System.Diagnostics.Debugger.Break();
            }
            Log.Info(e.ExceptionObject.Message);
            base.OnUnhandledException(sender, e);
        }

        static readonly ILog Log = LogManager.GetLog(typeof(object));
    }
}
